﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using trangbaocao.Models;
using System.Data;
using System.Data.OracleClient;
using Microsoft.Reporting.WebForms;

namespace trangbaocao.Reports.Fuel.FuelGenerator
{
    public partial class ReportGeneratorActivityProgressive : System.Web.UI.Page
    {
        StationsModel StationsModel = new StationsModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        //public string session_status = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                
                if (Session != null && Convert.ToInt32(Session["quyen_id"]) > 2)
                {
                    Radio1.Visible = false;
                }
                List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(main_StationsModel[0].id));
                cbtrungtam.DataSource = main_StationsModel;
                cbtrungtam.DataTextField = "name";
                cbtrungtam.DataValueField = "id";
                cbtrungtam.DataBind();

                cbtram.DataSource = stationModel;
                cbtram.DataTextField = "name";
                cbtram.DataValueField = "id";
                cbtram.DataBind();

                cbnam.DataSource = getYear();
                cbnam.DataTextField = "nam";
                cbnam.DataValueField = "nam";
                cbnam.DataBind();

                for (int i = 1; i <= 12; i++)
                {
                    cbthang.Items.Add(i.ToString());
                    cbthang2.Items.Add(i.ToString());
                }
                //cbthang.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                cbthang2.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                
            }
            //else
            //    if (Session != null)
            //    {
            //        session_status = Session["GeneratorFuel"].ToString();
            //        Session.Remove("GeneratorFuel");
            //    }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ReportViewer1.Reset();
            Main_Sort_DeviceModel Main_Sort_DeviceModel = new Main_Sort_DeviceModel();
            List<Main_Sort_DeviceModel> main_Sort_DeviceModel = Main_Sort_DeviceModel.getChungloaithietbi();

            ReportViewer1.LocalReport.ReportPath = "Reports/Fuel/FuelGenerator/ReportGeneratorActivityProgressive.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GeneratorActivityProgressive_dataset", getGeneratorActivityProgressive().Tables[0]));
            
            ReportViewer1.DataBind();
            ReportViewer1.LocalReport.Refresh();
        }

        protected void radio_tram_group_CheckedChanged(object sender, EventArgs e)
        {
            if (radio_tram_group.Checked)
            {
                cbtram.Enabled = false;
            }
            if (radio_tram.Checked)
            {
                cbtram.Enabled = true;
            }
        }

        protected void Radio1_CheckedChanged(object sender, EventArgs e)
        {
            if (Radio1.Checked)
            {
                cbtrungtam.Enabled = false;
                cbtram.Enabled = false;
            }
            if (Radio2.Checked)
            {
                cbtrungtam.Enabled = true;
                cbtram.Enabled = true;
            }
        }

        protected void cbtrungtam_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(cbtrungtam.SelectedValue));
            cbtram.DataSource = stationModel;
            cbtram.DataTextField = "name";
            cbtram.DataValueField = "id";
            cbtram.DataBind();
        }
        public DataSet getGeneratorActivityProgressive()
        {
            string sql = "";
            string nam = cbnam.SelectedValue;
            string thang = cbthang.SelectedValue;
            string thang2 = cbthang2.SelectedValue;
            string trung_tam_id = cbtrungtam.SelectedValue;
            string tram_id = cbtram.SelectedValue;

            string sql_tram = "";
            if (radio_tram.Checked)
                sql_tram = " and c.id='" + tram_id + "'";
            if (Radio2.Checked)
            {
                sql += " select b.ma_he_thong,b.name,c.name ten_tram,d.name ten_trung_tam,round(sum(a.thoi_gian_hd)/60,2) thoi_gian_hd,concat('THANG_',a.thang) thang";
                sql += " from fuel_detail a,device_stations b,stations c,main_stations d where ";
                sql += " a.device_stations_id=b.id and a.stations_id=c.id and a.main_stations_id=d.id and";
                sql += " a.thang BETWEEN " + thang + " and " + thang2 + " and a.nam = " + nam + " and d.id="+trung_tam_id+sql_tram+" group by a.thang,b.ma_he_thong,b.name,c.name,d.name";

            }
            if (Radio1.Checked)
            {
                //sql += " select a.device_stations_id, a.tong_nhap,b.tong_thoi_gian,ROUND(a.tong_nhap/b.tong_thoi_gian,2) dinh_muc,a.main_stations_id, a.stations_id";
                //sql += " FROM(SELECT a.device_stations_id, sum(a.nhap_trong_thang) tong_nhap,a.stations_id,a.main_stations_id";
                //sql += " FROM fuel a where  thang BETWEEN 1 and 2 and nam=2014 group by device_stations_id,main_stations_id,stations_id ) a,";
                //sql += " (SELECT a.device_stations_id, ROUND(sum(a.thoi_gian_hd)/60,2) tong_thoi_gian,a.main_stations_id, a.stations_id ";
                //sql += " FROM fuel_detail a where  thang BETWEEN "+thang+" and "+thang2+" and nam="+nam+" group by device_stations_id,main_stations_id,stations_id";
                //sql += " ) b where a.device_stations_id=b.device_stations_id";

                sql += " select b.ma_he_thong,b.name,c.name ten_tram,d.name ten_trung_tam,round(sum(a.thoi_gian_hd)/60,2) thoi_gian_hd,concat('THANG_',a.thang) thang";
                sql += " from fuel_detail a,device_stations b,stations c,main_stations d where ";
                sql += " a.device_stations_id=b.id and a.stations_id=c.id and a.main_stations_id=d.id and";
                sql += " a.thang BETWEEN " + thang + " and " + thang2 + " and a.nam = " + nam + " group by a.thang,b.ma_he_thong,b.name,c.name,d.name";
            }

            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }

        
        public DataSet getYear()
        {
            string sql = "SELECT distinct nam  from fuel_detail order by nam desc";
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
        

    }
}